package com.swsc.xapp.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import com.swsc.xapp.R;
import com.swsc.xapp.activity.mainActivity.MainActivity;

import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
import static android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC;

/**
 * 前台服务
 */
public class CustomForegroundService extends Service {

    private final String CHANNEL_ONE_ID = "100";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // 安卓引导用户设置APP运行后台保活1. 创建通知栏常驻通知
        initNotif("title", "text");
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        return super.onStartCommand(intent, flags, startId);
        //安卓引导用户设置APP运行后台保活2. 返回START_STICKY,被系统或手动清理后可重启
        return START_STICKY;
    }

    public void initNotif(String title, String context) {
        //RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.RemoteView);
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher, null);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        Bitmap bitmapIcon = getBitmapFromDrawable(drawable);

        Intent nfIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nfIntent, 0);
        @SuppressLint("WrongConstant")
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext(), CHANNEL_ONE_ID)
                // 设置PendingIntent
                .setContentIntent(pendingIntent)
                // 设置状态栏内的小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                // 设置大图标
                //.setLargeIcon(bitmapIcon)
                .setContentTitle(title)
                // 设置内容
                .setContentText(context)
                // 设置该通知发生的时间
                .setWhen(System.currentTimeMillis())
                .setVisibility(VISIBILITY_PUBLIC)// 锁屏显示全部通知
                //.setDefaults(Notification.DEFAULT_ALL)// //使用默认的声音、振动、闪光
                .setPriority(PRIORITY_HIGH);// 通知的优先级

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //修改安卓8.0以上系统报错
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, "服务常驻通知", NotificationManager.IMPORTANCE_MIN);
            //如果使用中的设备支持通知灯，则说明此通知通道是否应显示灯
            notificationChannel.enableLights(false);
            //是否显示角标
            notificationChannel.setShowBadge(false);
            //是否震动
            notificationChannel.enableVibration(false);
            //锁屏显示全部通知
            notificationChannel.setLockscreenVisibility(VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(notificationChannel);
            builder.setChannelId(CHANNEL_ONE_ID);
        }
        // 获取构建好的Notification
        Notification notification = builder.build();
        //设置为默认的声音
        notification.defaults = Notification.DEFAULT_SOUND;

        startForeground(1, notification);
    }

    /**
     * 把资源图片转换成Bitmap
     *
     * @param drawable 资源图片
     * @return 位图
     */
    public static Bitmap getBitmapFromDrawable(Drawable drawable) {
        int width = drawable.getBounds().width();
        int height = drawable.getBounds().height();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }
}
